﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace sys_Print.Converter
{
    public class StringToObservableCollectionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string stringValue)
            {
                // 将输入的字符串分割为单个项，然后创建一个ObservableCollection
                string[] items = stringValue.Split(',');
                return new ObservableCollection<string>(items);
            }

            return null; // 如果无法转换，返回null或空集合，具体取决于需求
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is ObservableCollection<string> collection)
            {
                // 将ObservableCollection合并为单个字符串，以逗号分隔
                return string.Join(",", collection);
            }

            return string.Empty; // 如果无法还原，返回空字符串或其他适当的值
        }
    }
}
